package com.zjw;

import com.zjw.entity.Admin;
import com.zjw.entity.User;
import com.zjw.service.AdminService;
import com.zjw.service.UserService;
import com.zjw.service.impl.AdminServiceImpl;
import com.zjw.service.impl.UserServiceImpl;

import java.util.Scanner;

public class ZzSystem {

    static Scanner input = new Scanner(System.in);
    static UserService userService = new UserServiceImpl();
    static AdminService adminService = new AdminServiceImpl();

    public static void main(String[] args) {
        menu();
    }

    /**
     * 菜单
     */
    public static void menu() {
        do {
            System.out.println("账号类型 : 1.普通用户  2.管理员");
            int type = input.nextInt();
            if (type == 1) {
                System.out.println("用户登录");
                UserMenu userMenu = new UserMenu(userLogin());
                userMenu.showUserMenu();
            } else if (type == 2) {
                System.out.println("管理员登录");
                AdminMenu adminMenu = new AdminMenu(adminLogin());
                adminMenu.showAdminMenu();
            } else {
                System.out.println("输入错误");
            }
        } while (true);
    }

    /**
     * 用户账号登录
     *
     * @return User
     */
    public static User userLogin() {
        System.out.println("请输入账号：");
        String username = input.next().trim();
        System.out.println("请输入密码：");
        String password = input.next().trim();
        User user = userService.login(username, password);
        if (user != null) {
            System.out.println("登录成功！");
            return user;
        } else {
            System.out.println("账号或密码输入错误，请重新输入");
            return userLogin();
        }
    }

    /**
     * 管理员登录
     *
     * @return Admin
     */
    public static Admin adminLogin() {
        System.out.println("请输入账号：");
        String adminName = input.next().trim();
        System.out.println("请输入密码：");
        String password = input.next().trim();
        Admin admin = adminService.login(new Admin(adminName, password));
        if (admin != null){
            return admin;
        }else{
            System.out.println("账号或密码输入错误，请重新输入");
            return adminLogin();
        }
    }


}
